package com.nsgf.library.ui.mine;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.kunminx.binding_recyclerview.adapter.BaseDataBindingAdapter;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.kunminx.binding_state.BR;
import com.nsgf.library.data.bean.AddressBean;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.utils.DialogUtil;
import com.nsgf.library.utils.ToastUtils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ActivityAddressListBinding;
import com.zhht.htzx.databinding.ItemAddressListBinding;

import java.util.ArrayList;
import java.util.List;

public class AddressListActivity extends BaseActivity {

    private AddressStates mStates;

    private AccountRequester mAccountRequester;

    private SimpleDataBindingAdapter<AddressBean, ItemAddressListBinding> mAdapter;

    private AddressBean addressBean;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(AddressStates.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    private ActivityAddressListBinding mBinding;

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new SimpleDataBindingAdapter<AddressBean, ItemAddressListBinding>(this,
                R.layout.item_address_list, new DiffUtil.ItemCallback<AddressBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull AddressBean oldItem, @NonNull AddressBean newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull AddressBean oldItem, @NonNull AddressBean newItem) {
                return false;
            }
        }) {
            @Override
            protected void onBindItem(ItemAddressListBinding binding, AddressBean item, RecyclerView.ViewHolder holder) {
                binding.setVm(item);

                binding.ivItemModifyAddress.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent()
                                .putExtra("data", item)
                                .setClass(AddressListActivity.this, AddAddressListActivity.class));
                    }
                });

                binding.ivItemDeleteAddress.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        DialogUtil dialogUtil = new DialogUtil(AddressListActivity.this);
                        dialogUtil.showConfirmDialog("删除收货地址",
                                "确认要删除该收货地址吗？",
                                "确认",
                                "取消",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        getController().displayLoading();
                                        mAccountRequester.deleteAddress(String.valueOf(item.getId()));
                                        dialogInterface.dismiss();

                                    }
                                }, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        dialogInterface.dismiss();
                                    }
                                });
                    }
                });
            }
        };
        return new DataBindingConfig(R.layout.activity_address_list, BR.vm, mStates)
                .addBindingParam(BR.click, new ClickProxy())
                .addBindingParam(BR.adapter, mAdapter);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mBinding = (ActivityAddressListBinding) getBinding();
        mStates.titleBar.set("收货地址");

        getLifecycle().addObserver(mAccountRequester);
        mAccountRequester.getAddressListResult().observe(this, new Observer<ResponseStatus<List<AddressBean>>>() {
            @Override
            public void onChanged(ResponseStatus<List<AddressBean>> listResponseStatus) {
                if (listResponseStatus.getResponseCode() == 0) {

                    mStates.addressList.set(listResponseStatus.getData());
                    if (mStates.addressList.get().isEmpty()) {
                        mBinding.llAddressNot.setVisibility(VISIBLE);
                    } else {
                        mBinding.llAddressNot.setVisibility(GONE);
                    }
                }
                mBinding.smartRefreshLayout.finishRefresh();
            }
        });

        mBinding.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                mAccountRequester.getAddressList();
            }
        });

        addressBean = getIntent().getParcelableExtra("data");

        mAdapter.setOnItemClickListener(new BaseDataBindingAdapter.OnItemClickListener<AddressBean>() {
            @Override
            public void onItemClick(int viewId, AddressBean item, int position) {
                Intent intent = new Intent();
                intent.putExtra("data", item);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        mAccountRequester.getSrcSuccess().observe(this, new Observer<ResponseStatus<String>>() {
            @Override
            public void onChanged(ResponseStatus<String> stringResponseStatus) {
                getController().dismissLoading();
                if (stringResponseStatus.getResponseCode() == 0) {
                    mBinding.smartRefreshLayout.autoRefresh();
                    ToastUtils.showShortToast(stringResponseStatus.getMsg());
                }
            }
        });
    }

    public static class AddressStates extends StateHolder {

        public final State<List<AddressBean>> addressList = new State<>(new ArrayList<>());

    }


    @Override
    protected void onResume() {
        super.onResume();
        mBinding.smartRefreshLayout.autoRefresh();
    }

    public class ClickProxy {
        public void AddAddress() {
            startActivity(new Intent().setClass(AddressListActivity.this, AddAddressListActivity.class));
        }
    }
}
